﻿using AdvancedTool;
using DiskToolSDK;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using static System.Console;

namespace SelfFeature
{
    [Creator("童泊森", true)]
    public class DeleteTool : IFileDoing
    {
        public override string About => "删除指定大小的文件";

        public override string[] FileExtends => null;

        public override bool UseExtend => false;
        long min;
        long max;
        long Min => min * 1024;
        long Max => max * 1024;
        long ReadUserInput()
        {
            long res = -1;
            if (long.TryParse(Console.ReadLine(), out res))
            {
                return res;
            }
            else
            {
                return ReadUserInput();
            }
        }
        public override void FileHandle(FileInfo file)
        {
            long size = file.Length;
            WriteLine("当前文件大小：{0}", file.Name);
            if (size >= Min && size <= Max)
            {
                File.Delete(file.FullName);
                WriteLine("已删除：{0},容量：{1:N2}kb", file.FullName, (double)size / 1024);
            }
        }
        public override void Init()
        {
            WriteLine("请输入文件大小，以kb作为单位");
            WriteLine("请输入被删除文件的最小大小：");
            min = ReadUserInput();
            WriteLine("请输入被删除文件的最大大小：");
            max = ReadUserInput();
        }
    }

    [Creator("童泊森",true)]
    public class RenameWork : IFileDoing
    {
        public override string About => "将文件进行有序重命名";

        public override string[] FileExtends => null;

        public override bool UseExtend => false;
        int index = 0;
        int mode = -1;
        public override void FileHandle(FileInfo file)
        {

            string p = "";
            switch (mode)
            {
                case 1:
                    Thread.Sleep(8);
                    DateTime date = DateTime.Now;
                    string datetimetamp = string.Format("{0}年{1}月{2}日{3}时{4}分{5}秒{6}毫秒", date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, date.Millisecond);
                    p = datetimetamp;
                    break;
                case 2:
                    p = index.ToString();
                    break;
                default:
                    throw new Exception("未知错误！");
            }
            string oldpath = file.FullName;
            string filepath = string.Format("{0}\\{1}{2}", file.DirectoryName, p, file.Extension);
            file.MoveTo(filepath);
            System.Console.WriteLine("{0}=>{1}", oldpath, filepath);
            index++;
        }
        int UserInput()
        {
            WriteLine();
            string p = ReadLine();
            if (p.Equals("1"))
            {
                return 1;
            }
            else if (p.Equals("2"))
            {
                return 2;
            }
            else
            {
                return UserInput();
            }
        }
        public override void Init()
        {
            ConsoleInput modeseletor = new ConsoleInput(getItem())
            {
                TitleText = "请选择模式：1.时间日期命名  2.序号命名"
            };
            mode = (int)modeseletor.UserInput();
        }
        Dictionary<string, object> getItem()
        {
            Dictionary<string, object> keys = new Dictionary<string, object>();
            for (int i = 1; i <= 2; i++)
            {
                keys.Add(i.ToString(), i);
            }
            return keys;
        }
    }

    [Creator("童泊森", true)]
    public class Md5Maker : IFileDoing
    {
        public override string About => "对指定文件或者文件夹进行MD5加密";

        public override string[] FileExtends => new string[0];

        public override bool UseExtend => false;

        public override void FileHandle(FileInfo file)
        {
            string code = DataProcess.Encryption.MD5Coder.MD5Encrypt(new FileStream(file.FullName, FileMode.Open)).ToUpper();
            WriteLine("文件：{0}\tMD5:{1}", file.FullName, code);
        }

        public override void Init()
        {

        }
    }
}
